package com.dongdong.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dongdong.pojo.TbItem;
import com.dongdong.pojo.TbOrderItem;
import com.dongdong.service.CartService;
import com.dongdong.service.ItemService;
import com.dongdong.vo.Cart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class CartServiceImpl implements CartService {
    @Reference
    private ItemService itemService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<Cart> addCart(List<Cart> cartList, Long itemId, Integer num) {
        // 根据itemId 查询item对象
        TbItem item = itemService.getItemById(itemId);
        //获取商家id
        String sellerId = item.getSellerId();
        //1,如果没有商家购物车数据:创建购物车
        Cart cart = getCartBySellerId(sellerId, cartList);
        if (cart == null){
            cart = new Cart();
            cart.setSellerId(sellerId);
            TbOrderItem orderItem = createOrderItem(item, num);
            ArrayList<TbOrderItem> orderItemList = new ArrayList<>();
            orderItemList.add(orderItem);
            //创建购物车明细
            cart.setOrderItemList(orderItemList);
            cartList.add(cart);
        }else {
            //2,如果有原来商家购物车数据，但没有买过一件商品，追加到购物车
            TbOrderItem orderItem = getOrderItemByitemId(cart.getOrderItemList(), itemId);
            if (orderItem == null){
                orderItem = createOrderItem(item, num);
                cart.getOrderItemList().add(orderItem);
            }else {
                //3,如果原来有商家购物车数据，且还有该商品，则增加商品数量和价格
                orderItem.setNum(orderItem.getNum()+num);//修改数量
                orderItem.setTotalFee(new BigDecimal(orderItem.getPrice().doubleValue()*orderItem.getNum()));
                if (orderItem.getNum() <= 0){
                    //如果数量被减完，将该水平中明细中移除
                    cart.getOrderItemList().remove(orderItem);
                }
                if (cart.getOrderItemList().size()<=0){
                   //如果明细中存在最后一个数量，再进行移除时购物车会被清空，此时我们需要移除购物车
                    cartList.remove(cart);
                }
            }
        }
        return cartList;
    }
    private  Cart getCartBySellerId(String sellerId,List<Cart> cartList){
        return   cartList.stream().filter(cart -> cart.getSellerId().equals(sellerId)).findFirst().orElse(null);
        /*for (Cart cart : cartList) {
            if (sellerId.equals(cart.getSellerId())){
                return cart;
            }
        }
        return null;*/
    }
    private TbOrderItem createOrderItem(TbItem item,Integer num){
        TbOrderItem orderItem = new TbOrderItem();
        orderItem.setNum(num);
        orderItem.setTitle(item.getTitle());
        orderItem.setPrice(item.getPrice());
        orderItem.setTotalFee(new BigDecimal(orderItem.getPrice().doubleValue()*num));//总价格
        orderItem.setPicPath(item.getImage());
        orderItem.setItemId(item.getId());
        orderItem.setGoodsId(item.getGoodsId());
        orderItem.setSellerId(item.getSellerId());
        return orderItem;
    }
    private TbOrderItem getOrderItemByitemId(List<TbOrderItem> orderItemList,Long itemId){
        return  orderItemList.stream().filter(orderItem -> orderItem.getItemId().equals(itemId)).findFirst().orElse(null);
       /* for (TbOrderItem orderItem : orderItemList) {
            if (itemId.equals(orderItem.getItemId())){
                return orderItem;
            }
        }
        return null;*/
    }
    @Override
    public List<Cart> findCartListRedis(String userName) {
        List<Cart> cartList = (List<Cart>) redisTemplate.boundHashOps("cartList").get(userName);
        return cartList == null ? new ArrayList<>():cartList;
    }

    @Override
    public void addCartToRedis(List<Cart> cartList, String userName) {
        redisTemplate.boundHashOps("cartList").put(userName,cartList);
    }
    @Override
    public List<Cart> merageCart(List<Cart> carListRdis, List<Cart> cartListCookie) {
        for (Cart cart : cartListCookie) {
            String sellerId = cart.getSellerId();
            List<TbOrderItem> orderItemList = cart.getOrderItemList();
            for (TbOrderItem orderItem : orderItemList) {
                carListRdis= addCart(carListRdis, orderItem.getItemId(), orderItem.getNum());
            }
        }
        return carListRdis;
    }
}
